package com.design.working.controller;

import com.design.working.config.BaseServlet;
import com.design.working.dto.PageControlDTO;
import com.design.working.dto.StudentWorkApplicationDTO;
import com.design.working.dto.StudentWorkMotionDTO;
import com.design.working.dto.student.UpdateIndividualInformationDTO;
import com.design.working.service.StudentService;
import com.design.working.service.impl.StudentServiceImpl;
import com.google.gson.Gson;
import org.apache.log4j.Logger;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * 学生端控制器
 *
 * @author gavin
 * @date 2022/5/9 10:48
 */

@WebServlet(name = "StudentController",
        urlPatterns = {"/student/*"})
public class StudentController extends BaseServlet {

    private final StudentService studentService = new StudentServiceImpl();
    Logger log = Logger.getLogger(UserController.class);

    /**
     * （申请岗位）查找全部岗位
     */
    public void selectJob(HttpServletRequest request, HttpServletResponse response) {

        try {
            request.setCharacterEncoding("UTF-8");
            response.setContentType("text/html; charset=UTF-8");
            HttpSession session = request.getSession();

            Integer uid = (Integer) session.getAttribute("uid");
            Gson gson = new Gson();
            String place = request.getParameter("place");
            // 获取分页参数
            PageControlDTO pageControlDTO = new PageControlDTO();
            pageControlDTO.setPage(Integer.valueOf(request.getParameter("page")));
            pageControlDTO.setLimit(Integer.valueOf(request.getParameter("limit")));

            response.getWriter().print(gson.toJson(studentService.selectJob(place, uid, pageControlDTO)));

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * （申请岗位）学生点击选择按钮申请岗位
     */
    public void applyWork(HttpServletRequest request, HttpServletResponse response) {

        try {
            request.setCharacterEncoding("UTF-8");
            response.setContentType("text/html; charset=UTF-8");
            HttpSession session = request.getSession();

            // 获取学生学号
            Integer uid = (Integer) session.getAttribute("uid");

            // 接收前端学生填写的申请数据
            StudentWorkApplicationDTO studentWorkApplicationDTO = new StudentWorkApplicationDTO(
                    Integer.parseInt(request.getParameter("id")),
                    request.getParameter("phone"),
                    request.getParameter("email"),
                    request.getParameter("politicalStatus"),
                    Integer.parseInt(request.getParameter("needyStudent")),
                    request.getParameter("dorm"),
                    request.getParameter("strength"),
                    request.getParameter("freeTime"));

            response.getWriter().print(studentService.insertStudentWorkApplication(uid, studentWorkApplicationDTO));

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    /**
     * （我的岗位）查找学生现属岗位
     */
    public void selectCurrentWork(HttpServletRequest request, HttpServletResponse response) {

        try {
            request.setCharacterEncoding("UTF-8");
            response.setContentType("text/html; charset=UTF-8");
            HttpSession session = request.getSession();
            Gson gson = new Gson();

            // 获取学生学号
            Integer uid = (Integer) session.getAttribute("uid");

            // 输出学生在岗岗位
            response.getWriter().print(gson.toJson(studentService.selectCurrentWork(uid)));

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 查询学生所有的用工岗位
     */
    public void selectAllWork(HttpServletRequest request, HttpServletResponse response) {

        try {
            request.setCharacterEncoding("UTF-8");
            response.setContentType("text/html; charset=UTF-8");
            HttpSession session = request.getSession();
            Gson gson = new Gson();

            // 获取学生学号
            Integer uid = (Integer) session.getAttribute("uid");
            // 获取分页参数
            PageControlDTO pageControlDTO = new PageControlDTO();
            pageControlDTO.setPage(Integer.valueOf(request.getParameter("page")));
            pageControlDTO.setLimit(Integer.valueOf(request.getParameter("limit")));
            response.getWriter().print(gson.toJson(studentService.selectAllWork(uid, pageControlDTO)));

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * （我的岗位）查询学生的考勤记录
     */
    public void selectAttendanceById(HttpServletRequest request, HttpServletResponse response) {

        try {
            request.setCharacterEncoding("UTF-8");
            response.setContentType("text/html; charset=UTF-8");
            Gson gson = new Gson();

            // 获取工作id
            Integer id = Integer.valueOf(request.getParameter("id"));
            // 获取分页参数
            PageControlDTO pageControlDTO = new PageControlDTO();
            pageControlDTO.setPage(Integer.valueOf(request.getParameter("page")));
            pageControlDTO.setLimit(Integer.valueOf(request.getParameter("limit")));
            response.getWriter().print(gson.toJson(studentService.selectAttendanceById(id, pageControlDTO)));

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * （我的岗位）岗位变更申请
     */
    public void applyWorkMotion(HttpServletRequest request, HttpServletResponse response) {

        try {
            request.setCharacterEncoding("UTF-8");
            response.setContentType("text/html; charset=UTF-8");

            // 接收前端表单信息
            StudentWorkMotionDTO studentWorkMotionDTO = new StudentWorkMotionDTO(
                    Integer.valueOf(request.getParameter("id")),
                    Integer.valueOf(request.getParameter("workType")),
                    request.getParameter("explanation"));

            response.getWriter().print(studentService.insertStudentWorkMotion(studentWorkMotionDTO));

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * （我的岗位）岗位申请记录
     */
    public void selectAllWorkApplication(HttpServletRequest request, HttpServletResponse response) {

        try {
            request.setCharacterEncoding("UTF-8");
            response.setContentType("text/html; charset=UTF-8");
            HttpSession session = request.getSession();
            Gson gson = new Gson();

            // 获取学号
            Integer uid = (Integer) session.getAttribute("uid");
            // 获取分页参数
            PageControlDTO pageControlDTO = new PageControlDTO();
            pageControlDTO.setPage(Integer.valueOf(request.getParameter("page")));
            pageControlDTO.setLimit(Integer.valueOf(request.getParameter("limit")));
            response.getWriter().print(gson.toJson(studentService.selectAllWorkApplication(uid, pageControlDTO)));

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * （工资查询）工资查询
     */
    public void selectSalary(HttpServletRequest request, HttpServletResponse response) {

        try {
            request.setCharacterEncoding("UTF-8");
            response.setContentType("text/html; charset=UTF-8");
            HttpSession session = request.getSession();
            Gson gson = new Gson();

            // 获取学号
            Integer uid = (Integer) session.getAttribute("uid");
            // lastMonth-查询上月工资；all-查询历史全部工资
            String type = request.getParameter("type");

            // 获取分页参数
            PageControlDTO pageControlDTO = new PageControlDTO();
            pageControlDTO.setPage(Integer.valueOf(request.getParameter("page")));
            pageControlDTO.setLimit(Integer.valueOf(request.getParameter("limit")));
            response.getWriter().print(gson.toJson(studentService.selectSalary(uid, type, pageControlDTO)));

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 通过学号查询个人信息
     */
    public void selectIndividualInformationByUid(HttpServletRequest request, HttpServletResponse response) {

        try {
            request.setCharacterEncoding("UTF-8");
            response.setContentType("text/html; charset=UTF-8");
            HttpSession session = request.getSession();

            Gson gson = new Gson();

            // 获取学号
            Integer uid = (Integer) session.getAttribute("uid");

            response.getWriter().print(gson.toJson(studentService.selectIndividualInformationByUid(uid)));

        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 通过学号更新个人信息
     */
    public void updateIndividualInformationByUid(HttpServletRequest request, HttpServletResponse response) {

        try {
            request.setCharacterEncoding("UTF-8");
            response.setContentType("text/html; charset=UTF-8");
            HttpSession session = request.getSession();

            // 获取学号
            Integer uid = (Integer) session.getAttribute("uid");

            // 获取表单数据
            UpdateIndividualInformationDTO updateIndividualInformationDTO = new UpdateIndividualInformationDTO();
            updateIndividualInformationDTO.setPhone(request.getParameter("phone"));
            updateIndividualInformationDTO.setEmail(request.getParameter("email"));
            updateIndividualInformationDTO.setNeedyStudent(Integer.valueOf(request.getParameter("needyStudent")));
            updateIndividualInformationDTO.setDorm(request.getParameter("dorm"));
            updateIndividualInformationDTO.setPoliticalStatus(request.getParameter("politicalStatus"));
            updateIndividualInformationDTO.setStrength(request.getParameter("strength"));

            response.getWriter().print(studentService.updateIndividualInformationByUid(uid, updateIndividualInformationDTO));

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
